#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: login
    Description:
        Author: lhw
        Date: 2020/7/6
-----------------------------------
"""

from rest_framework.views import APIView
from rest_framework.response import Response
from Common.jwt_auth import create_token
from Common.logger import gen_log
from Common import bcrypt
from Common.response import resp
from Admin import models
from extensions.auth import JwtAuthorizationAuthentication


# Create your views here.


class LoginView(APIView):

    def post(self, request, *args, **kwargs):
        try:
            email = request.data.get('email')
            password = request.data.get('password')

        except Exception as e:
            gen_log.error(e)
            return Response({'status_code': 1001, "msg": 'params error'})

        try:
            user = models.AdminUser.objects.filter(email=email).values("id", "email", "name", "hospital", "encrypted_password",
                                                                       "role__title").first()
            if user:
                is_pass = bcrypt.Bcrypt.check(password, user["encrypted_password"])
                if is_pass:
                    user_info = {
                        "user": user["id"],
                        "name": user["name"],
                        "email": user["email"],
                        "hospital": user["hospital"],
                        "role": user["role__title"]
                    }
                    token = create_token({"email": user["email"]})
                    data = {"user_info": user_info,
                            "token": token}
                    # return Response({'status_code': 1000, "msg": "ok", "data": user_info, "token": token})
                    return resp(0, data)
                else:
                    # return Response({'status_code': 1002, "msg": "wrong password"})
                    return resp(3003, {})
            else:
                # return Response({"status_code": 1003, "msg": "email does not exist"})
                return resp(4001, {})
        except Exception as e:
            gen_log.error(e)
            return resp(-1, e)
            # return Response({"status_code": 1005, "msg": "login failed"})


class ResetPwdView(APIView):
    authentication_classes = [JwtAuthorizationAuthentication, ]

    def post(self, request, *args, **kwargs):
        try:
            email = request.data.get("email", "")
            new_password = request.data.get("new_password", "")
            user = models.AdminUser.objects.filter(email=email).first()
            if user:
                user.encrypted_password = bcrypt.Bcrypt.encrypt(new_password)
                user.save()
                return Response({
                    'error_code': 0,
                    'message': "Password Reset Success"
                })
        except Exception as e:
            gen_log.error(e)
            return resp(-1, e.args)
